import {Component, OnInit} from '@angular/core';
import {CustomerManageService} from '../customer-manage.service';
import {Router} from '@angular/router';
import {NzMessageService} from 'ng-zorro-antd/message';
import {PermissionService} from '../../../service/permission.service';

@Component({
  selector: 'app-email-manage',
  templateUrl: './email-manage.component.html',
  styleUrls: ['./email-manage.component.scss']
})
export class EmailManageComponent implements OnInit {
  statusOptions = [
    {id: 0, name: '草稿'},
    {id: 1, name: '待自动发送'},
    {id: 2, name: '已发送'},
    {id: 3, name: '已撤回'},
    {id: 4, name: '已取消'},
    {id: 5, name: '待审核'},
    {id: 6, name: '审核不通过'},
  ];
  query = {
    insideTitle: '', // 内部标题
    title: '', // 标题
    date: [],	// 发送时间
    state: [], // 0草稿 1待自动发送 2已发送 3已撤回 4已取消 5待审核 6审核不通过
    page: 1,  // 当前页
    pageSize: 20,  // 分页大小
  };
  listOfData = {
    records: [],
    total: 0,
  };
  checkModal = { // 审核模态框
    id: '',
    remark: '',
    visible: false,
    loading: false,
  };

  constructor(
    public customerManageService: CustomerManageService,
    private router: Router,
    private nzMessageService: NzMessageService,
    public permission: PermissionService,
  ) {
  }

  ngOnInit(): void {
    this.queryTableList();
  }

  // 页码变化
  onPageIndexChange(index: number) {
    this.query.page = index;
    this.queryTableList();
  }

  // 页面大小变化
  onPageSizeChange(size: number) {
    this.query.page = 1;
    this.query.pageSize = size;
    this.queryTableList();
  }

  // 查询按钮点击
  queryBtn() {
    this.query.page = 1;
    this.queryTableList();
  }

  // 重置查询条件
  resetCondition() {
    this.query = {
      insideTitle: '',
      title: '',
      date: [],
      state: [],
      page: 1,
      pageSize: 20,
    };
  }

  // 查询列表
  queryTableList() {
    this.customerManageService.httpPost(this.customerManageService.serviceUrl.emailList, {
      insideTitle: this.query.insideTitle, // 内部标题
      title: this.query.title, // 标题
      beginTime: this.customerManageService.formatTime(this.query.date[0], 'date'),
      endTime: this.customerManageService.formatTime(this.query.date[1], 'date'),
      state: this.query.state.length ? this.query.state.join(',') : '', // 0草稿 1待自动发送 2已发送 3已撤回 4已取消 5待审核 6审核不通过
      page: this.query.page,
      pageSize: this.query.pageSize,
    }).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  // 去详情
  toDetail(item: any) {
    this.router.navigate(['/customer-manage/send-emails'], {queryParams: {id: item.id, disabled: 'disabled'}});
  }

  // 去编辑
  toEdit(item: any) {
    this.router.navigate(['/customer-manage/send-emails'], {queryParams: {id: item.id}});
  }

  // 取消
  cancelEmail(id: any) {
    this.changeStatus(id, 4);
  }

  // 撤回
  withdrawEmail(id: any) {
    this.customerManageService.httpPost(this.customerManageService.serviceUrl.withdraw, {id}).then((res: any) => {
      if (res.code === 0) {
        this.queryTableList();
        this.nzMessageService.success('操作成功');
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  // 提交审核
  submitCheck(id: any) {
    this.changeStatus(id, 5);
  }

  // 改变状态
  changeStatus(id: any, state: number) {
    this.customerManageService.httpPost(this.customerManageService.serviceUrl.emailCheck, {
      id,
      state,
      auditDesc: ''
    }).then((res: any) => {
      if (res.code === 0) {
        this.queryTableList();
        this.nzMessageService.success('操作成功');
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  // 显示审核模态框
  checkModalShow(id: any) {
    this.checkModal.id = id;
    this.checkModal.visible = true;
  }

  // 隐藏审核模态框
  checkModalHide() {
    this.checkModal = {
      id: '',
      remark: '',
      visible: false,
      loading: false,
    };
  }

  // 审核-拒绝
  checkReject() {
    if (!this.checkModal.remark) {
      this.nzMessageService.warning('拒绝时请输入审核意见');
      return;
    }
    this.checkModal.loading = true;
    this.customerManageService.httpPost(this.customerManageService.serviceUrl.emailCheck, {
      id: this.checkModal.id,
      state: 6,
      auditDesc: this.checkModal.remark
    }).then((res: any) => {
      if (res.code === 0) {
        this.queryTableList();
        this.nzMessageService.success('操作成功');
        this.checkModalHide();
      } else {
        this.nzMessageService.error(res.message);
        this.checkModal.loading = false;
      }
    }).finally(() => {
      this.checkModal.loading = false;
    });
  }

  // 审核-通过
  checkResolve() {
    this.checkModal.loading = true;
    this.customerManageService.httpPost(this.customerManageService.serviceUrl.emailCheck, {
      id: this.checkModal.id,
      state: 1,
      auditDesc: ''
    }).then((res: any) => {
      if (res.code === 0) {
        this.queryTableList();
        this.nzMessageService.success('操作成功');
        this.checkModalHide();
      } else {
        this.nzMessageService.error(res.message);
        this.checkModal.loading = false;
      }
    }).finally(() => {
      this.checkModal.loading = false;
    });
  }

  // 提前终止
  async advanceStop(item: any) {
    let params = {
      id: item.id,
      state: 8 // 0草稿 1待自动发送 2已发送 3已撤回 4已取消 5待审核 6审核不通过 7进行中 8已终止
    }
    const res: any = await this.customerManageService.httpPost(this.customerManageService.serviceUrl.advanceStopUrl, params);
    if (res.code == 0) {
      this.nzMessageService.success('操作成功');
      this.queryTableList();
    } else {
      this.nzMessageService.error(res.message || '操作失败');
    }
  }
}
